struct directional_light {
	vec3 position;			
	vec3 halfplane;			
	vec4 ambient_color;  // make global
	vec4 diffuse_color;
	vec4 specular_color;
};

struct spot_light {
	vec3 position;
	vec3 direction;
	vec3 halfplane; 
	float exponent;
	float angle;
	vec4 ambient_color;  // make global
	vec4 diffuse_color;
	vec4 specular_color;
};


struct material_properties {
	vec4 ambient_color;
	vec4 diffuse_color;
	vec4 specular_color;
	float specular_exponent;
};


uniform material_properties u_material;
//uniform directional_light u_light;


uniform spot_light u_light;
uniform spot_light u_light1;
uniform spot_light u_light2;
uniform spot_light u_light3;
uniform spot_light u_light4;
uniform spot_light u_light5;
uniform spot_light u_light6;
uniform spot_light u_light7;


uniform int u_light_num;



uniform vec4 u_global_ambient_color;

uniform mat4 u_mvp_matrix;
uniform mat4 u_modelview_matrix;
uniform mat4 u_inv_modelview_matrix;

uniform float u_lightingMode;

//u_shadowToggle = 1 -> Schatten zeichnen
uniform int u_shadowToggle;
uniform mat4 l_mvp_matrix;


attribute vec4 a_position;
attribute vec3 a_normal;
attribute vec2 a_tc;

varying vec3 v_normal;
varying vec4 v_color;
varying vec2 v_tc;
varying vec3 v_position;
varying float v_cel_intensity;

varying vec4 v_shadow_coord;

const float c_null = 0.0;

//+++LIGHT CALCULATION+++//

vec4 lightCalc(vec4 normal, vec4 position, vec3 lightPosition, vec3 halfplane, vec4 diffuseL, vec4 specularL){

	vec3 norm_lightvector = normalize(lightPosition);

	vec4 computed_color;
	
	//	vec3 real_halfplane = normalize(norm_lightvector + vec3(0.0, 0.0, 1.0));
			
	vec3 norm_normal = normalize(normal.xyz);
	float LdotN = dot(norm_lightvector, norm_normal);
	float HdotN = dot(norm_normal, halfplane);
	float Irradiance = max(0.0, LdotN);
	vec4 diffuse_term = Irradiance * (diffuseL * u_material.diffuse_color);
	vec4 specular_term = pow(max(0.0, HdotN), u_material.specular_exponent) * (specularL * u_material.specular_color);
		
	computed_color = diffuse_term + specular_term;
	return computed_color;

}

void main() {

	vec4 temp_normal = u_inv_modelview_matrix * vec4(a_normal, 0.0);

	//Gouraud-lighting
	if(u_lightingMode == 1.0){
		vec4 temp_color = lightCalc(temp_normal, a_position, u_light.position, u_light.halfplane, u_light.diffuse_color, u_light.specular_color);
		if(u_light_num >= 2){
			temp_color += lightCalc(temp_normal, a_position, u_light1.position, u_light1.halfplane, u_light1.diffuse_color,	u_light1.specular_color);
			
			if(u_light_num >= 3){
				temp_color += lightCalc(temp_normal, a_position, u_light2.position, u_light2.halfplane, u_light2.diffuse_color, u_light2.specular_color);
				
				if(u_light_num >= 4){
					temp_color += lightCalc(temp_normal, a_position, u_light3.position, u_light3.halfplane, u_light3.diffuse_color, u_light3.specular_color);
					
					if(u_light_num >= 5){
						temp_color += lightCalc(temp_normal, a_position, u_light4.position, u_light4.halfplane, u_light4.diffuse_color, u_light4.specular_color);
						
						if(u_light_num >= 6){
							temp_color += lightCalc(temp_normal, a_position, u_light5.position, u_light5.halfplane, u_light5.diffuse_color, u_light5.specular_color);
								
							if(u_light_num >= 7){
								temp_color += lightCalc(temp_normal, a_position, u_light6.position, u_light6.halfplane, u_light6.diffuse_color, u_light6.specular_color);
								
								if(u_light_num == 8){
									temp_color += lightCalc(temp_normal, a_position, u_light7.position, u_light7.halfplane, u_light7.diffuse_color, u_light7.specular_color);
									}
								}
							}
						}
					}
				}
			}

		temp_color += (u_global_ambient_color * u_material.ambient_color);
		v_color = temp_color;
		
		}


	
	//Phong-lighting & Cel-shading
	else if(u_lightingMode == 2.0 || u_lightingMode == 3.0){
		v_normal = temp_normal.xyz;
		}	
	
	
	
	if(u_shadowToggle == 1){
		v_shadow_coord = l_mvp_matrix * a_position;
		}


gl_Position = u_mvp_matrix * a_position;

v_tc = a_tc;
v_position = (u_mvp_matrix * a_position).xyz;

}
